// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Ending Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 39 : TYPE_ENDINGSETUP


// Static Values

// Tables

private table GHZwateroriginal
	160, 0, 128, 224
	168, 96, 128, 160
	169, 96, 128, 224
	170, 128, 160, 224
	171, 160, 192, 224
end table

private table GHZwaterforever
	160, 32, 96, 224
	168, 64, 96, 224
	169, 96, 128, 224
	170, 128, 160, 224
	171, 160, 192, 224
end table

private table GHZwatertitle
	160, 0, 0, 128
	168, 32, 64, 192
	169, 96, 128, 224
	170, 160, 192, 224
	171, 192, 224, 224
end table

event ObjectMain
	if object.state < 5
		object.value7++
		if object.value7 == 6
			object.value7 = 0
			RotatePalette(0, 168, 171, 1)
		end if
	end if
	if object.value0 == 0
		object.value0 = 20
		temp0 = 768
		temp0 += object.frame
		Copy16x16Tile(427, temp0)
		temp0++
		Copy16x16Tile(428, temp0)
		temp0++
		Copy16x16Tile(429, temp0)
		temp0++
		Copy16x16Tile(430, temp0)
		temp0 = 796
		temp0 += object.frame
		Copy16x16Tile(421, temp0)
		temp0++
		Copy16x16Tile(422, temp0)
		temp0++
		Copy16x16Tile(423, temp0)
		temp0++
		Copy16x16Tile(424, temp0)
		object.frame += 4
		object.frame &= 7
	else
		object.value0--
	end if
	if object.value1 == 0
		switch object.value2
		case 0
			object.value1 = 160
			temp0 = 0
			break
		case 1
			object.value1 = 10
			temp0 = 6
			break
		case 2
			object.value1 = 160
			temp0 = 12
			break
		case 3
			object.value1 = 10
			temp0 = 6
			break
		end switch
		temp0 += 776
		Copy16x16Tile(431, temp0)
		temp0++
		Copy16x16Tile(432, temp0)
		temp0++
		Copy16x16Tile(433, temp0)
		temp0++
		Copy16x16Tile(434, temp0)
		temp0++
		Copy16x16Tile(435, temp0)
		temp0++
		Copy16x16Tile(436, temp0)
		object.value2++
		object.value2 &= 3
	else
		object.value1--
	end if
	if object.value3 == 0
		switch object.value4
		case 0
			object.value3 = 16
			temp0 = 0
			break
		case 1
			object.value3 = 16
			temp0 = 6
			break
		case 2
			object.value3 = 16
			temp0 = 12
			break
		case 3
			object.value3 = 16
			temp0 = 6
			break
		end switch
		temp0 += 816
		Copy16x16Tile(411, temp0)
		temp0++
		Copy16x16Tile(412, temp0)
		temp0++
		Copy16x16Tile(413, temp0)
		temp0++
		Copy16x16Tile(414, temp0)
		temp0++
		Copy16x16Tile(415, temp0)
		temp0++
		Copy16x16Tile(416, temp0)
		object.value4++
		object.value4 &= 3
	else
		object.value3--
	end if
	if object.value5 == 0
		switch object.value6
		case 0
			object.value5 = 16
			temp0 = 0
			break
		case 1
			object.value5 = 16
			temp0 = 4
			break
		case 2
			object.value5 = 16
			temp0 = 8
			break
		case 3
			object.value5 = 16
			temp0 = 4
			break
		end switch
		temp0 += 804
		Copy16x16Tile(417, temp0)
		temp0++
		Copy16x16Tile(418, temp0)
		temp0++
		Copy16x16Tile(419, temp0)
		temp0++
		Copy16x16Tile(420, temp0)
		object.value6++
		object.value6 &= 3
	else
		object.value5--
	end if
end event


event ObjectStartup
	SetMusicTrack("Ending.ogg", 0, 0)
	animalType1 = TypeName[Pocky]
	animalType2 = TypeName[Flicky]
	object[10].type = TypeName[Ending Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].value6 = 8
	temp0 = 424
	temp0 -= screen.xcenter
	stage.newXBoundary1 = temp0

	if saved.superStates == 0
		temp1 = 63
	else
		temp1 = 127
	end if
	
	if stage.debugMode == 1
		specialStage.emeralds = temp1
	end if

	if specialStage.emeralds < temp1
		temp0 = tileLayer[0].xsize
		temp0 <<= 7
		temp0 -= 256
		stage.curXBoundary2 = temp0
	else
		stage.curXBoundary2 = 0x700
	end if
	stage.curYBoundary1 = 264
	stage.curYBoundary2 = 504
	if options.stageSelectFlag == 0
		saveRAM[43] |= 1
		if specialStage.emeralds >= 63
			saveRAM[44] |= 1
		end if
		saveRAM[45] = 20
	end if
	WriteSaveRAM()

	switch saved.GHZwater
		case 1 //Forever
			temp5 = GHZwaterforever
			break
		case 2 //Title Screen
			temp5 = GHZwatertitle
			break
		default
			temp5 = GHZwateroriginal
			break
	end switch
	
	
	temp0 = 0
	while temp0 < 20
		GetTableValue(temp1, temp0, temp5)
		temp0++
		GetTableValue(temp2, temp0, temp5)
		temp0++
		GetTableValue(temp3, temp0, temp5)
		temp0++
		GetTableValue(temp4, temp0, temp5)
		temp0++
		
		temp2 <<= 8
		temp2 += temp3
		temp2 <<= 8
		temp2 += temp4				
		SetPaletteEntry(0, temp1, temp2)
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
